package com.zyp.jobseeker.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zyp.jobseeker.entity.User;
import com.zyp.jobseeker.enums.ApplicationStatus;
import com.zyp.jobseeker.enums.InterviewStatus;
import com.zyp.jobseeker.enums.UserStatus;
import com.zyp.jobseeker.mapper.ApplicationMapper;
import com.zyp.jobseeker.mapper.InterviewMapper;
import com.zyp.jobseeker.mapper.UserMapper;
import com.zyp.jobseeker.service.AdminService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class AdminServiceImpl implements AdminService {

    private static final Logger logger = LoggerFactory.getLogger(AdminServiceImpl.class);

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private InterviewMapper interviewMapper;
    @Autowired
    private ApplicationMapper applicationMapper;
    /**
     * 获取待审核学生列表
     */
    @Override
    public PageInfo<User> getPendingStudents(int page, int size) {
        logger.info("[管理员审核] 开始查询待审核学生列表，页码: {}, 每页数量: {}", page, size);

        PageHelper.startPage(page, size);
        List<User> students = userMapper.findPendingStudents(
                (page - 1) * size, size);

        PageInfo<User> pageInfo = new PageInfo<>(students);
        logger.info("[管理员审核] 待审核学生列表查询完成，返回结果数: {}", pageInfo.getTotal());
        return pageInfo;
    }

    /**
     * 获取待审核企业列表
     */
    @Override
    public PageInfo<User> getPendingEnterprises(int page, int size) {
        logger.info("[管理员审核] 开始查询待审核企业列表，页码: {}, 每页数量: {}", page, size);

        PageHelper.startPage(page, size);
        List<User> enterprises = userMapper.findPendingEnterprises(
                (page - 1) * size, size);

        PageInfo<User> pageInfo = new PageInfo<>(enterprises);
        logger.info("[管理员审核] 待审核企业列表查询完成，返回结果数: {}", pageInfo.getTotal());
        return pageInfo;
    }

    /**
     * 审核用户注册申请
     */
    @Transactional
    @Override
    public boolean auditUserRegistration(Long userId, int userStatus) {
        logger.info("[管理员审核] 开始审核用户注册，用户ID: {}, 状态: {}",
                userId, UserStatus.getByCode(userStatus).getDesc());

        try {
            // 验证用户是否存在
            User user = userMapper.selectById(userId);
            if (user == null) {
                logger.warn("[管理员审核] 用户不存在，ID: {}", userId);
                return false;
            }

            // 验证当前状态是否为待审核
            if (user.getStatus().getCode() != UserStatus.PENDING.getCode()) {
                logger.warn("[管理员审核] 用户已审核，ID: {}", userId);
                return false;
            }

            // 更新用户状态
            int result = userMapper.updateStatusById(userId, userStatus);
            if (result > 0) {
                logger.info("[管理员审核] 用户审核完成，用户ID: {}, 状态: {}",
                        userId, UserStatus.getByCode(userStatus).getDesc());
                return true;
            }
            logger.error("[管理员审核] 用户审核失败，数据库未更新，ID: {}", userId);
            return false;

        } catch (Exception e) {
            logger.error("[管理员审核] 用户审核异常", e);
            throw new RuntimeException("用户审核失败: " + e.getMessage());
        }
    }
    @Override
    public PageInfo<Map<String, Object>> getAllApplications(int page, int size) {
        PageHelper.startPage(page, size);
        List<Map<String, Object>> list = applicationMapper.findAllApplications();
        for (Map<String, Object> map : list) {
            Object statusObj = map.get("status");
            if (statusObj != null) {
                int code = (statusObj instanceof Integer) ? (Integer) statusObj : Integer.parseInt(statusObj.toString());
                map.put("status", ApplicationStatus.getByCode(code).name());
            }
        }
        return new PageInfo<>(list);
    }

    @Override
    public PageInfo<Map<String, Object>> getAllInterviews(int page, int size) {
        PageHelper.startPage(page, size);
        List<Map<String, Object>> list = interviewMapper.findAllInterviews();
        for (Map<String, Object> map : list) {
            Object statusObj = map.get("status");
            if (statusObj != null) {
                int code = (statusObj instanceof Integer) ? (Integer) statusObj : Integer.parseInt(statusObj.toString());
                map.put("status", InterviewStatus.getByCode(code).name());
            }
        }
        return new PageInfo<>(list);
    }
}